PepSim.define('PepSim.Food', {

	init: function (world, left, top, energy) {
		world.entities.append(this);
		this.world = world;
		this.worldWidth = world.width;
		this.worldHeight = world.height;
		
		this.left = left;
		this.top = top;
		
		this.world.put(this.left, this.top, this);
				
		this.energy = energy || 200;
		this.type = 'food';
		
		this.speed = 10;
		this.time = this.speed;
	},
	
	toString : function () {
		return this.type + '. Energy: ' + this.energy;
	},
	
	kill : function () {
		this.phantom = true;
		this.world.clear(this.left, this.top);
	},
	
	spread : function (hDirection, vDirection) {
		var newTop = this.top + vDirection,
			newLeft = this.left + hDirection,
			quantum = Math.floor(this.energy / 6) + 1,
			newItem;
		
		if (quantum > 50) {
			quantum = 50;
		}
		if (this.energy > quantum * 2) {
			if (
				newTop < this.worldHeight &&
				newTop >= 0 &&
				newLeft < this.worldWidth &&
				newLeft >= 0)
			{
				newItem = this.world.get(newLeft, newTop);
				if (!newItem) {
					newItem = new PepSim.Food(this.world, newLeft, newTop, quantum);
					this.energy = this.energy - quantum;
				} else if (newItem.type === 'food') {
					newItem.energy = newItem.energy + quantum;
					this.energy = this.energy - quantum;
				}
			}
		}
	},
	
	play: function () {
		if (this.energy > 200) {
			this.energy = 200;
		}
		this.time = this.time - 1;
		if (this.time < 1) {
			this.time = this.speed;
			this.spread(-1, 0);
			this.spread(0, -1);
			this.spread(0, 1);
			this.spread(1, 0);
		}

		//this.energy = this.energy - 1;
		if (this.energy <= 1) {
			this.kill();
		}
	}

});